package org.gabryh.android.clipboard;

import static org.gabryh.android.clipboard.Provider.*;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.TextView;

public class ClipShell extends Activity {
	
	private static final int LIST = Menu.FIRST;

	private static final int INSERT = LIST+1;
	
	private static TextView textView ;
	private static StringBuffer sb = new StringBuffer();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        if(textView == null){
	        textView = new TextView(this); 
	        appendMessage(sb.toString()) ;
        }else{
        	appendMessage("!!! Create again !!!");        	
        }
        textView.append("First Created\n") ;
        Log.i("CB", "First Created") ;
        setContentView(textView);
    }
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	appendMessage("Paused") ;
    	super.onPause();
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	appendMessage("Resumed") ;
    	super.onResume();
    }
    
    public static void appendMessage(String msg){
    	msg = msg + "\n" ;
    	if(textView == null){
    		sb.append(msg) ;
    	}else{
    		textView.append(msg);
    	}
    	Log.i("ClipShell", msg) ;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, LIST, R.string.manu_list);
        menu.add(0, INSERT, R.string.manu_insert);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, Item item) {
        super.onMenuItemSelected(featureId, item);
        switch(item.getId()) {
	        case LIST:
	        	appendMessage("List Selected") ;
	        	Cursor cursor = managedQuery(	Provider.CONTENT_URI_LIST, 
	        									null, // Retrieve all columns
	        									null, // Retrieve all rows
	        									null, // 
	        									null) ; // No special ordering
	        	
	        	appendMessage("Clip Count:" + cursor.count()) ;
	        	
	        	break ;
	        
	        case INSERT:
	        	appendMessage("Insert Clicked") ;
	        	
	        	ContentValues values = new ContentValues() ;
	        	
	        	values.put(COLUMN_TEXTUAL_DATA, "Text DATA");
	        	values.put(COLUMN_CONTENT_URI, "URI DATA");
	        	
	        	getContentResolver().insert(Provider.CONTENT_URI, values) ;
	        	break;
        }
        
        return true;
    }
    
}